package com.cjx.auth.controller;

import com.nimbusds.jose.jwk.JWKSet;
import com.nimbusds.jose.jwk.RSAKey;
import com.nimbusds.jose.util.Base64URL;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.KeyPair;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Map;

/**
*@Description 获取jwt公钥接口
*@Verson v1.0.0
*@Author cjunxian
*@Date 
*/
@Api(tags = "获取公钥接口")
@RestController
@RequestMapping("/rsa")
@AllArgsConstructor
@Slf4j
public class PublicKeyController {

    @Autowired
    @Qualifier("jwtKeyPair")
    private KeyPair keyPair;

    @GetMapping("/getPublicKey")
    public Map<String, Object> loadPublicKey() {
        //RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
        //System.out.println(Base64URL.encode(privateKey.getPrivateExponent()));
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        RSAKey key = new RSAKey.Builder(publicKey).build();
        return new JWKSet(key).toJSONObject();
    }

}
